'use strict';
const db = uniCloud.database()
const accountCollection = db.collection("Accounts")

exports.main = async (event, context) => {
	try {
		const existUser = await accountCollection.where({
			name: event.name
		}).get();
		if (existUser.data.length > 0) {
			return {
				code: 400,
				msg: "该账号已注册"
			};
		}
		const insertResult = await accountCollection.add({
			nickname: event.nickname,
			email: event.email,
			phone: event.phone,
			name: event.name,
			password: event.password,
			createTime: event.createTime,
			createDate: event.createDate,
			status: event.status
		});
		console.log("[云函数 addUser]注册成功，ID：", insertResult.id);
		return {
			code: 200,
			msg: "注册成功",
			data: insertResult
		};
	} catch (err) {
		console.error("[云函数 addUser]注册失败：", err);
		return {
			code: 500,
			msg: "注册失败：" + err.message
		};
	}
};